''' <summary>
''' Decode XML created by Stations web service, getStationsForMap routine
''' </summary>
''' <remarks></remarks>
Public Class clsDataCuahsiSta
    Inherits clsData
    Implements IDisposable

    Private mDataSet As New DataSet
    Private mDataTable As New DataTable 'flat-file table
    Private mNumRecords As Integer

    ''' <summary>
    ''' Instantiate clsData, assign filename, open file, set filelength and fieldnames
    ''' </summary>
    ''' <param name="_AreaOfInterest">Name of text file to read</param>
    Public Overrides Sub Initialize(ByVal _AreaOfInterest As String)
        MyBase.Initialize(_AreaOfInterest)
        mDataType = enumDataType.CuashiSta

        mFieldNames.Add("Station_ID")
        mFieldNames.Add("Station_Name")
        mFieldNames.Add("Station_Type")
        mFieldNames.Add("Agency")
        mFieldNames.Add("Agency_Name")
        mFieldNames.Add("Latitude")
        mFieldNames.Add("Longitude")

        For Each s As String In mFieldNames
            mDataTable.Columns.Add(s, GetType(String))
        Next

        Using fs = New IO.FileStream(mFilename, IO.FileMode.Open)
            Dim mode As Data.XmlReadMode = mDataSet.ReadXml(fs)
            'must crawl through relations to gather up all data in dataset, converting hierachical dataset into flat data file for import

            'Stations web service, getStationsForMap routine

            If mDataSet.Tables.Count > 0 Then
                For Each drOrganization As DataRow In mDataSet.Tables("Organization").Rows
                    Dim drOrganizationDescription As DataRow = drOrganization.GetChildRows("Organization_OrganizationDescription")(0) '1 to 1 relationship
                    Dim AgencyID As String = TestNull(drOrganizationDescription.Item("OrganizationIdentifier"), "")
                    Dim AgencyName As String = TestNull(drOrganizationDescription.Item("OrganizationFormalName"), "")
                    For Each drMonitoringLocation As DataRow In drOrganization.GetChildRows("Organization_MonitoringLocation")
                        Dim MonLocIdent As String = ""
                        Dim MonLocName As String = ""
                        Dim MonLocType As String = ""
                        Dim MonLocID As String = ""
                        Dim Latitude As String = ""
                        Dim Longitude As String = ""
                        For Each drMonitoringLocationIdentity As DataRow In drMonitoringLocation.GetChildRows("MonitoringLocation_MonitoringLocationIdentity")
                            MonLocIdent = TestNull(drMonitoringLocationIdentity.Item("MonitoringLocationIdentifier"), "")
                            MonLocName = TestNull(drMonitoringLocationIdentity.Item("MonitoringLocationName"), "")
                            MonLocType = TestNull(drMonitoringLocationIdentity.Item("MonitoringLocationTypeName"), "")
                            MonLocID = TestNull(drMonitoringLocationIdentity.Item("MonitoringLocation_ID"), "")
                        Next
                        For Each drMonitoringLocationGeospatial As DataRow In drMonitoringLocation.GetChildRows("MonitoringLocation_MonitoringLocationGeospatial")
                            Latitude = TestNull(drMonitoringLocationGeospatial.Item("LatitudeMeasure"), "")
                            Longitude = TestNull(drMonitoringLocationGeospatial.Item("LongitudeMeasure"), "")
                        Next
                        Dim Station_ID As String = String.Format("{0}+{1}", MonLocIdent.Trim, AgencyID.Trim)
                        mDataTable.Rows.Add(Station_ID, MonLocName.Trim, MonLocType.ToUpper.Trim, AgencyID.ToUpper.Trim, AgencyName.Trim, Latitude.Trim, Longitude.Trim)
                    Next
                Next
            End If
        End Using
        mNumRecords = mDataTable.Rows.Count
        mActiveTable = IO.Path.GetFileNameWithoutExtension(mFilename)
        mTableNames.Add(mActiveTable)
        ActiveTable = mActiveTable 'to force it to get the fieldnames
    End Sub

    ''' <summary>
    ''' Read next line from file into memory, storing into mItems array
    ''' Also save mLastLineRead for error reporting
    ''' </summary>
    ''' <returns>True if successful, false if eof reached and no more data</returns>
    Public Overrides Function Read() As Boolean
        Try
            If Not MyBase.Read() Or mDataTable.Rows.Count = 0 Then Return False
            mItems.Clear()
            For c As Integer = 0 To mDataTable.Columns.Count - 1
                mItems.Add(mDataTable.Rows(mRecordNum).Item(c))
            Next
            Return mRecordNum < mDataTable.Rows.Count - 1
        Catch ex As Exception
            ErrorMsg(, ex)
            Return False
        End Try
    End Function

    ''' <summary>
    ''' Determine percent of file that has been read
    ''' </summary>
    ''' <returns>Percent of file read</returns>
    Public Overrides Function PercentComplete() As Double
        Return (mRecordNum + 1) * 100.0 / mNumRecords
    End Function

    Private disposed As Boolean = False

    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        If Not Me.disposed Then
            If disposing Then
                mDataTable.Dispose()
                mDataSet.Dispose()
            End If
        End If
        Me.disposed = True
    End Sub


End Class
